package be.bonamis.umpire.ui.client.mvp.impl;

import java.util.Date;
import java.util.List;
import java.util.Set;

import be.bonamis.projects.umpire.business.dto.LeagueDayDTO;
import be.bonamis.projects.umpire.business.dto.UmpireDTO;
import be.bonamis.umpire.ui.client.mvp.DispoView;
import be.bonamis.umpire.ui.client.resources.Resources;
import be.credoc.common.gwt.idhandler.client.ViewIdHandler;

import com.google.gwt.cell.client.ActionCell;
import com.google.gwt.cell.client.DateCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.IdentityColumn;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.view.client.ListDataProvider;
import com.google.inject.Inject;

public class DispoViewImpl extends Composite implements DispoView {

	private Presenter presenter;
	private UmpireDTO umpire;
	private Set<Date> months;
	private long leagueDayId;

	interface DispoViewImplUiBinder extends UiBinder<HTMLPanel, DispoViewImpl> {
	}
	
	@UiField
	Anchor dispoButton;
	
	@UiField
	Anchor whoButton;
	
	@UiField
	Anchor quizButton;
	
	@UiField
	Anchor historyButton;
	
	@UiField
	Label fullName;
	
	@UiField
	SimplePanel simplePanel;
	
	@UiHandler("dispoButton")
	public void onClickDispo(ClickEvent event) {
		presenter.dispoButtonClicked();
	}
	
	@UiHandler("whoButton")
	public void onClickWho(ClickEvent event) {
		presenter.whoButtonClicked();
	}
	
	@UiHandler("quizButton")
	public void onClickQuiz(ClickEvent event) {
		presenter.quizButtonClicked();
	}
	
	@UiHandler("historyButton")
	public void onClickHistory(ClickEvent event) {
		presenter.historyButtonClicked();
	}


	@UiField
	CellTable<Date> monthTable;

	@UiField
	CellTable<LeagueDayDTO> daysTable;

	@UiField
	CellTable<UmpireDTO> umbiresByLDTable;	
	
	@UiField
	Button monthListButton;

	@UiField
	Button daysListButton;

	@UiField
	Button printXlsButton;	

	@UiHandler("monthListButton")
	public void onClickMonth(ClickEvent event) {		
		monthListButton.setFocus(true);
		monthListButton.setFocus(false);
		monthListButton.setVisible(false);
		monthTable.setVisible(true);
		daysTable.setVisible(false);
		umbiresByLDTable.setVisible(false);
		daysListButton.setVisible(false);
		printXlsButton.setVisible(false);
	}

	@UiHandler("daysListButton")
	public void onClickDays(ClickEvent event) {
		monthTable.setVisible(false);
		daysListButton.setFocus(true);
		daysListButton.setVisible(false);		
		daysTable.setVisible(true);
		umbiresByLDTable.setVisible(false);		
		printXlsButton.setVisible(false);		
	}

	@UiHandler("printXlsButton")
	public void onClickPrint(ClickEvent event) {
		presenter.exportToXls(leagueDayId);
	}

	private ListDataProvider<Date> monthDataProvider;
	private ListDataProvider<LeagueDayDTO> daysDataProvider;
	private ListDataProvider<UmpireDTO> umpiresDataProvider;

	private static IdHandler idHandler = GWT.create(IdHandler.class);

	interface IdHandler extends ViewIdHandler<DispoViewImpl> {
	}

	DateTimeFormat daysFormat = DateTimeFormat.getFormat(PredefinedFormat.DATE_FULL);
	DateTimeFormat monthsFormat = DateTimeFormat.getFormat("MMMM yyyy");

	@Inject
	public DispoViewImpl(DispoViewImplUiBinder uiBinder, final Resources resources) {
		initWidget(uiBinder.createAndBindUi(this));
		resources.css().ensureInjected();
		idHandler.setIds(this);
		daysTable.setVisible(false);
		umbiresByLDTable.setVisible(false);

		TextColumn<Date> dateCol = new TextColumn<Date>() {
			@Override
			public String getValue(Date date) {
				return monthsFormat.format(date);
			}
		};

		ActionCell<Date> actionCell = new ActionCell<Date>("Click Me",
				new ActionCell.Delegate<Date>() {
					@Override
					public void execute(Date date) {
						monthTable.setVisible(false);
						String month = DateTimeFormat.getFormat("M").format(date);
						String year = DateTimeFormat.getFormat("y").format(date);
						presenter.findLeagueDays(umpire.getId(),
								Integer.parseInt(month), Integer.parseInt(year));
					}
				});

		IdentityColumn<Date> column = new IdentityColumn<Date>(actionCell);
		monthTable.addColumn(dateCol);
		monthTable.addColumn(column);
		monthTable.setColumnWidth(dateCol, 200, Unit.PX);

		monthDataProvider = new ListDataProvider<Date>();
		monthDataProvider.addDataDisplay(monthTable);

		DateCell dateCell = new DateCell(daysFormat);
		Column<LeagueDayDTO, Date> dateColumn = new Column<LeagueDayDTO, Date>(dateCell) {
			@Override
			public Date getValue(LeagueDayDTO object) {
				return object.getDay();
			}
		};
		daysTable.addColumn(dateColumn);

		TextColumn<LeagueDayDTO> descrColumn = new TextColumn<LeagueDayDTO>() {
			@Override
			public String getValue(LeagueDayDTO object) {
				return object.getGameType();
			}
		};
		daysTable.addColumn(descrColumn);

		boolean admin = true;
		if (admin) {
			ActionCell<LeagueDayDTO> showmUmpiresCell = new ActionCell<LeagueDayDTO>("Umpires",
					new ActionCell.Delegate<LeagueDayDTO>() {
						@Override
						public void execute(LeagueDayDTO dto) {
							daysTable.setVisible(false);
							leagueDayId = dto.getId();
							presenter.findUmpiresByLeagueDay(dto.getId());
						}
					});

			IdentityColumn<LeagueDayDTO> showmUmpiresCellColumn = new IdentityColumn<LeagueDayDTO>(
					showmUmpiresCell);
			daysTable.addColumn(showmUmpiresCellColumn);
		}

		daysDataProvider = new ListDataProvider<LeagueDayDTO>();
		daysDataProvider.addDataDisplay(daysTable);

		TextColumn<UmpireDTO> nameColumn = new TextColumn<UmpireDTO>() {
			@Override
			public String getValue(UmpireDTO dto) {
				return (dto.getLastName() + " " + dto.getFirstName());
			}
		};

		TextColumn<UmpireDTO> rankingColumn = new TextColumn<UmpireDTO>() {
			@Override
			public String getValue(UmpireDTO dto) {
				return dto.getRanking();
			}
		};
		umbiresByLDTable.setVisibleRange(0, 9999);
		umbiresByLDTable.addColumn(nameColumn);
		umbiresByLDTable.addColumn(rankingColumn);

		umpiresDataProvider = new ListDataProvider<UmpireDTO>();
		umpiresDataProvider.addDataDisplay(umbiresByLDTable);
		umbiresByLDTable.setVisible(false);
	}

	public UmpireDTO getUmpire() {
		return umpire;
	}

	public Set<Date> getMonths() {
		return months;
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}

	@Override
	public Presenter getPresenter() {
		return presenter;
	}

	@Override
	public void setValues(final UmpireDTO umpire, Set<Date> months) {
		this.umpire = umpire;
		this.months = months;
		
		fullName.setText(umpire.getFirstName() +" " + umpire.getLastName());

		List<Date> list = monthDataProvider.getList();
		for (Date date : months) {
			list.add(date);
		}		
		monthTable.setVisible(true);
	}

	@Override
	public void setLeagueDays(List<LeagueDayDTO> leagueDayDtoList) {
		List<LeagueDayDTO> list = daysDataProvider.getList();
		for (LeagueDayDTO leagueDayDTO : leagueDayDtoList) {
			list.add(leagueDayDTO);
		}		
		daysTable.setVisible(true);
		monthListButton.setVisible(true);
		daysListButton.setVisible(false);
	}

	@Override
	public void setUmpiresByLD(List<UmpireDTO> umpiresDTOList) {
		List<UmpireDTO> list = umpiresDataProvider.getList();
		for (UmpireDTO umpireDTO : umpiresDTOList) {
			list.add(umpireDTO);
		}		
		umbiresByLDTable.setVisible(true);
		daysListButton.setVisible(true);
		printXlsButton.setVisible(true);		
	}
}
